// comps/authSetting/authSetting.js
const md5 = require('../../utils/md5.js')
const common = require('../../utils/util.js')
const app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isUserInfo: Boolean,
        isPhone: Boolean,
        isLogin: Boolean
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {
        getUserInfo: function(e) {
            const that = this
            if (e.detail.errMsg == 'getUserInfo:ok') {
                wx.showLoading({
                    title: '登录中...',
                })
                wx.checkSession({
                    success() {
                        //session_key 未过期，并且在本生命周期一直有效
                        console.log('session_key 未过期，并且在本生命周期一直有效')
                        let data = {}
                        let key = wx.getStorageSync('token')
                        let sign = 'ydy'
                        let timestamp = Date.parse(new Date())
                        data.key = key
                        data.timestamp = timestamp
                        data = that.objKeySort(data)
                        for (let i in data) {
                            sign += (i + data[i])
                        }
                        sign += 'ydy'
                        sign = md5(sign)
                        data.sign = sign
                        data.code = wx.getStorageSync('code')
                        data.phone = wx.getStorageSync('userPhone')
                        data.wx_sm = 1
                        data.encryptedData = e.detail.encryptedData
                        data.iv = e.detail.iv
                        wx.request({
                            url: 'https://yidianyuan.bailingyoupin.com/api/Login/phone_unionid',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            method: 'POST',
                            data: data,
                            success: function(res) {
                                wx.login({
                                    success(loginRes) {
                                        wx.setStorageSync('code', loginRes.code)
                                    }
                                })
                                const data = res.data.datas
                                if (res.data.code == 204) {
                                    wx.hideLoading()
                                    wx.showModal({
                                        title: '提示',
                                        content: '网络繁忙，请重新登录',
                                        showCancel: false,
                                        success(res) {}
                                    })
                                } else {
                                    wx.hideLoading()
                                    wx.setStorageSync('isUserInfo', true)
                                    wx.setStorageSync('unionID', 1)
                                    common.refresh()
                                }
                            }
                        })
                    },
                    fail() {
                        // session_key 已经失效，需要重新执行登录流程
                        console.log('session_key 已经失效，需要重新执行登录流程')
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    wx.setStorageSync('code', res.code)
                                    //发起网络请求
                                    let data = {}
                                    let key = wx.getStorageSync('token')
                                    let sign = 'ydy'
                                    let timestamp = Date.parse(new Date())
                                    data.key = key
                                    data.timestamp = timestamp
                                    data = that.objKeySort(data)
                                    for (let i in data) {
                                        sign += (i + data[i])
                                    }
                                    sign += 'ydy'
                                    sign = md5(sign)
                                    data.sign = sign
                                    data.code = res.code
                                    data.phone = wx.getStorageSync('userPhone')
                                    data.wx_sm = 1
                                    data.encryptedData = e.detail.encryptedData
                                    data.iv = e.detail.iv
                                    wx.request({
                                        url: 'https://yidianyuan.bailingyoupin.com/api/Login/phone_unionid',
                                        header: {
                                            'content-type': 'application/x-www-form-urlencoded'
                                        },
                                        method: 'POST',
                                        data: data,
                                        success: function(res) {
                                            const data = res.data.datas
                                            if (res.data.code == 204) {
                                                wx.hideLoading()
                                                wx.showModal({
                                                    title: '提示',
                                                    content: '网络繁忙，请重新登录',
                                                    showCancel: false,
                                                    success(res) {}
                                                })
                                            } else {
                                                wx.hideLoading()
                                                wx.setStorageSync('isUserInfo', true)
                                                wx.setStorageSync('unionID', 1)
                                                common.refresh()
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            }
            else{
                wx.setStorageSync('isUserInfo', true)
                common.refresh()
            }
        },
        
        getPhoneNumber(e) {
            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                wx.showLoading({
                    title: '登录中...',
                })
                wx.checkSession({
                    success() {
                        //session_key 未过期，并且在本生命周期一直有效
                        console.log('session_key 未过期，并且在本生命周期一直有效')
                        let loginData
                        if (wx.getStorageSync('userShare')) {
                            loginData = {
                                encryptedData: e.detail.encryptedData,
                                iv: e.detail.iv,
                                code: wx.getStorageSync('code'),
                                belong_refer: wx.getStorageSync('userShare')
                            }
                        } else {
                            loginData = {
                                encryptedData: e.detail.encryptedData,
                                iv: e.detail.iv,
                                code: wx.getStorageSync('code')
                            }
                        }
                        wx.request({
                            url: 'https://yidianyuan.bailingyoupin.com/api/Login/phone_bing',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            method: 'POST',
                            data: loginData,
                            success: function(res) {
                                wx.login({
                                    success(loginRes) {
                                        wx.setStorageSync('code', loginRes.code)
                                    }
                                })
                                const data = res.data.datas
                                if (res.data.code == 204) {
                                    wx.hideLoading()
                                    wx.showModal({
                                        title: '提示',
                                        content: '网络繁忙，请重新登录',
                                        showCancel: false,
                                        success(res) {

                                        }
                                    })
                                } else {
                                    wx.hideLoading()
                                    wx.setStorageSync('loginStatus', false)
                                    wx.setStorageSync('isLogin', true)
                                    wx.setStorageSync('token', data.token)
                                    wx.setStorageSync("roleType", data.role_type)
                                    wx.setStorageSync("storeId", data.store_id)
                                    wx.setStorageSync("userId", data.user_id)
                                    wx.setStorageSync("userPhone", data.phone)
                                    wx.setStorageSync("belongId", data.belong_refer)
                                    wx.setStorageSync("belongType", data.parent_role_type)
                                    if (data.is_unoid == 0) {
                                        wx.setStorageSync('isUserInfo', false)
                                        wx.setStorageSync('unionID', 0)
                                    }
                                    else{
                                        wx.setStorageSync('isUserInfo', true)
                                        wx.setStorageSync('unionID', 1)
                                    }
                                    common.refresh()
                                }
                            }
                        })
                    },
                    fail() {
                        // session_key 已经失效，需要重新执行登录流程
                        console.log('session_key 已经失效，需要重新执行登录流程')
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    wx.setStorageSync('code', res.code)
                                    //发起网络请求
                                    let loginData
                                    if (wx.getStorageSync('userShare')) {
                                        loginData = {
                                            encryptedData: e.detail.encryptedData,
                                            iv: e.detail.iv,
                                            code: res.code,
                                            belong_refer: wx.getStorageSync('userShare')
                                        }
                                    } else {
                                        loginData = {
                                            encryptedData: e.detail.encryptedData,
                                            iv: e.detail.iv,
                                            code: res.code
                                        }
                                    }
                                    wx.request({
                                        url: 'https://yidianyuan.bailingyoupin.com/api/Login/phone_bing',
                                        header: {
                                            'content-type': 'application/x-www-form-urlencoded'
                                        },
                                        method: 'POST',
                                        data: loginData,
                                        success: function(res) {
                                            wx.login({
                                                success(loginRes) {
                                                    wx.setStorageSync('code', loginRes.code)
                                                }
                                            })
                                            const data = res.data.datas
                                            if (res.data.code == 204) {
                                                wx.hideLoading()
                                                wx.showModal({
                                                    title: '提示',
                                                    content: '网络繁忙，请重新登录',
                                                    showCancel: false,
                                                    success(res) {

                                                    }
                                                })
                                            } else {
                                                wx.hideLoading()
                                                wx.setStorageSync('loginStatus', false)
                                                wx.setStorageSync('isLogin', true)
                                                wx.setStorageSync('token', data.token)
                                                wx.setStorageSync("roleType", data.role_type)
                                                wx.setStorageSync("storeId", data.store_id)
                                                wx.setStorageSync("userId", data.user_id)
                                                wx.setStorageSync("userPhone", data.phone)
                                                if (data.is_unoid == 0) {
                                                    wx.setStorageSync('isUserInfo', false)
                                                    wx.setStorageSync('unionID', 0)
                                                }
                                                else {
                                                    wx.setStorageSync('isUserInfo', true)
                                                    wx.setStorageSync('unionID', 1)
                                                }
                                                common.refresh()
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            } else {
                wx.navigateTo({
                    url: '/pages/user/bindPhone/bindPhone'
                })
            }
        },
        myCatchTouch: function() {
            return
        },
        objKeySort: function(obj) {
            let newkey = Object.keys(obj).sort()
            let newObj = {}
            for (let i = 0; i < newkey.length; i++) {
                newObj[newkey[i]] = obj[newkey[i]]
            }
            return newObj
        }
    }
})